﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Utils.Extensions;

namespace StockAnalyst2.StockGrid
{
	internal partial class GridViewer : UserControl
	{
		#region Properties

		public new GridVM DataContext
		{
			get
			{
				return (GridVM)base.DataContext;
			}
			set
			{
				if (this.DataContext != value)
				{
					base.DataContext = value;
				}
			}
		}

		#endregion

		#region Public Methods

		public GridViewer()
		{
			InitializeComponent();
		}

		#endregion

		#region Event Handlers

		private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			this.LastDateColumn.SetFixedWidth(this.LastDateColumn.ActualWidth);
			this.GroupColumn.MinWidth = this.LastDateColumn.ActualWidth * 1.1;
			this.NameColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 1);
			this.SymbolColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.8);
			this.ValueColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.5);
			this.OpenDiffColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.8);
			this.PrevDiffColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.95);
			this.BidVolumeColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.8);
			this.BidColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.55);
			this.AskColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.55);
			this.AskVolumeColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.8);
			this.GapDiffColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.8);
			this.BidAskAverageColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.8);
			this.MaxColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.45);
			this.MinColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.45);
			this.LastVolumeColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.8);
			this.VolumeColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.65);
			this.LastOperationColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.9);
			this.MaxDiffColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 1);
			this.MinDiffColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 1);
			this.OpenColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.55);
			this.PrevDayColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.7);
			this.StatusColumn.SetFixedWidth(this.LastDateColumn.ActualWidth * 0.7);
			this.GroupColumn.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
		}

		#endregion

		#region Event Handlers

		// Hack to avoid that a hidden grid reverts the changes on the column headers of a visible one. Only the visible grid should have the displayindex binding set to TwoWay.
		private void MainGrid_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			if (this.IsVisible)
			{
				foreach (DataGridColumn column in this.MainGrid.Columns)
				{
					Binding currentBinding = BindingOperations.GetBinding(column, DataGridColumn.DisplayIndexProperty);
					if (currentBinding != null && currentBinding.Mode != BindingMode.TwoWay)
					{
						Binding newBinding = new Binding();
						newBinding.Source = currentBinding.Source;
						newBinding.FallbackValue = currentBinding.FallbackValue;
						newBinding.Path = currentBinding.Path;
						newBinding.Mode = BindingMode.TwoWay;

						BindingOperations.ClearBinding(column, DataGridColumn.DisplayIndexProperty);
						BindingOperations.SetBinding(column, DataGridColumn.DisplayIndexProperty, newBinding);
					}
				}
			}
			else
			{
				foreach (DataGridColumn column in this.MainGrid.Columns)
				{
					Binding currentBinding = BindingOperations.GetBinding(column, DataGridColumn.DisplayIndexProperty);
					if (currentBinding != null && currentBinding.Mode != BindingMode.OneWay)
					{
						Binding newBinding = new Binding();
						newBinding.Source = currentBinding.Source;
						newBinding.FallbackValue = currentBinding.FallbackValue;
						newBinding.Path = currentBinding.Path;
						newBinding.Mode = BindingMode.OneWay;
						
						BindingOperations.ClearBinding(column, DataGridColumn.DisplayIndexProperty);
						BindingOperations.SetBinding(column, DataGridColumn.DisplayIndexProperty, newBinding);
					}
				}
			}
		}

		#endregion
	}
}
